package de.bht.mi.kriz.multitouch;


import de.bht.mi.kriz.multitouch.components.MultiTouchViewElement;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Style;

public class SquareElement extends MultiTouchViewElement {

	public SquareElement(Context context) {
		super(context);
		initializeElement(context);
	}
	
	public SquareElement(Context context, Point p) {
		super(context, p);
		initializeElement(context);
	}
	
	public SquareElement(Context context, int x, int y) {
		super(context, x, y);
		initializeElement(context);
	}	
	
	@Override
	protected void initializeElement(Context context) {
		drawColor = 0xff00ff00;
		TITLE_SPACE = 0;
		MIN_SIZE = 5;
		super.initializeElement(context);
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		super.onDraw(canvas);
		canvas.translate(globalPosition.x, globalPosition.y);
		
		Paint pathPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
		pathPaint.setStyle(Style.FILL);
		pathPaint.setColor(this.drawColor);
		pathPaint.setAlpha(getAlpha());
		
		Path pth = new Path();
		pth.moveTo(0, TITLE_SPACE);
		pth.lineTo(0, TITLE_SPACE+height);
		pth.lineTo(width, TITLE_SPACE+height);
		pth.lineTo(width, TITLE_SPACE);
		pth.close();
		canvas.drawPath(pth, pathPaint);
		
		canvas.translate(-globalPosition.x, -globalPosition.x);
	}

	
	//checks which view-component-part is at the world coordinate located point
	public int check(float x, float y) 
	{
		Point absoultePos = this.getAbsolutePosition();
		
		if(x > absoultePos.x-GRAB_TOLLERANCE && x < absoultePos.x+width+GRAB_TOLLERANCE && y > absoultePos.y-GRAB_TOLLERANCE && y < absoultePos.y+height+GRAB_TOLLERANCE)
		{
			return MOVE_HANDLE;
		}
		
		return super.check(x, y);
	}
}